import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_demo/other/application.dart';
import 'package:flutter_demo/widget/buttons.dart';
import 'package:fluttertoast/fluttertoast.dart';

class EditCompanyInfoScreen extends StatelessWidget {

  final GlobalKey _formKey = GlobalKey<FormState>();
  final TextEditingController _levelController = TextEditingController();
  final TextEditingController _natureController = TextEditingController();
  final TextEditingController _industryController = TextEditingController();
  final TextEditingController _clientController = TextEditingController();
  final TextEditingController _locationController = TextEditingController();
  final TextEditingController _addressController = TextEditingController();
  final TextEditingController _remarksController = TextEditingController();

  @override
  Widget build(BuildContext context) {

    var level = Padding(
      padding: const EdgeInsets.fromLTRB(64, 32, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _levelController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '单位级别',
            hintText: '您的单位级别',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '单位级别不能为空';
            } else if (value.trim().length > 4) {
              return '单位级别超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var nature = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _natureController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '单位性质',
            hintText: '您的单位性质',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '单位性质不能为空';
            } else if (value.trim().length > 5) {
              return '单位性质超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var industry = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _industryController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '单位行业',
            hintText: '您的单位行业',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '单位行业不能为空';
            } else if (value.trim().length > 5) {
              return '单位行业超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var client = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _clientController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '单位委托人名称',
            hintText: '您的单位委托人名称',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '单位委托人名称不能为空';
            } else if (value.trim().length > 4) {
              return '单位委托人名称超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var location = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _locationController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '公司所在地',
            hintText: '您的公司所在地',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '公司所在地不能为空';
            } else if (value.trim().length > 25) {
              return '公司所在地超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var address = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _addressController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '公司地址',
            hintText: '您的公司地址',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '公司地址不能为空';
            } else if (value.trim().length > 25) {
              return '公司地址超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var remarks = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _remarksController,
          keyboardType: TextInputType.phone,
          decoration: const InputDecoration(
            labelText: '备注说明',
            hintText: '您的备注说明',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '备注说明不能为空';
            } else if (value.trim().length > 50) {
              return '备注说明超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var bt_commit = Padding(
      padding: EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: BaseButton(
        radius: 12,
        onTap: () {
          if ((_formKey.currentState as FormState).validate())
            validatePassed(context);
        },
        child: Text(
          '完成',
          style: TextStyle(
            fontSize: 16,
            color: Colors.white,
          ),
        ),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: const Text('修改公司信息'),
      ),
      body: Scrollbar(
        child: SingleChildScrollView(
          child: Form(
            key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                level,
                nature,
                industry,
                client,
                location,
                address,
                remarks,
                bt_commit,
              ],
            ),
          ),
        ),
      ),
    );
  }

  void validatePassed(BuildContext context) async {

    companyInfoData['单位级别'] = _levelController.text;
    companyInfoData['单位性质'] = _natureController.text;
    companyInfoData['单位行业'] = _industryController.text;
    companyInfoData['委托人'] = _clientController.text;
    companyInfoData['公司所在地'] = _locationController.text;
    companyInfoData['公司地址'] = _addressController.text;
    companyInfoData['备注说明'] = _remarksController.text;

    Timer(Duration(milliseconds: 500), (){
      Fluttertoast.showToast(msg: '提交成功');
      Navigator.pop(context);
    });
  }
}